﻿<template>
  <a-modal
    :title="title"
    width="70%"
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleSubmit"
    @cancel="
      () => {
        this.visible = false
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-model-item label="服务项目编码" prop="Number">
          <a-input v-model="entity.Number" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="服务项目" prop="Name">
          <a-input v-model="entity.Name" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="服务详细内容" prop="Content">
          <!-- <a-input v-model="entity.Content" autocomplete="off" /> -->
          <!-- <a-card :bordered="false"> -->
            <wang-editor v-model="entity.Content"></wang-editor>
          <!-- </a-card> -->
        </a-form-model-item>
        <a-form-model-item label="提供服务部门" prop="DepartmentId">
          <a-tree-select
            v-model="entity.DepartmentId"
            allowClear
            :treeData="DepartmentIdTreeData"
            placeholder="请选择部门"
            treeDefaultExpandAll
          ></a-tree-select>
        </a-form-model-item>
        <a-form-model-item label="项目单价" prop="Price">
          <a-input v-model="entity.Price" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="科目名称" prop="AccountId">
          <a-select v-model="entity.AccountId" placeholder="请选择科目">
            <a-select-option v-for="item in AccountIdData" :key="item.Id">{{ item.Name }}</a-select-option>
          </a-select>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import WangEditor from '@/components/WangEditor/WangEditor'
export default {
  props: {
    parentObj: Object,
  },
  components: {
    WangEditor
  },
  data() {
    return {
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 },
      },
      visible: false,
      loading: false,
      entity: {},
      DepartmentIdTreeData: [],
      AccountIdData: [],
      title: '',
      rules: {
        Number: [{ required: true, message: '必填' }],
        Name: [{ required: true, message: '必填' }],
        Content: [{ required: true, message: '必填' }],
        DepartmentId: [{ required: true, message: '必填' }],
        Price: [{ required: true, message: '必填' }],
        AccountId: [{ required: true, message: '必填' }],
      }
    }
  },
  methods: {
    init() {
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
      this.$http.post('/Base_Manage/Base_Department/GetTreeDataList', {}).then((resJson) => {
        if (resJson.Success) {
          this.DepartmentIdTreeData = resJson.Data
        }
      })
      this.$http.post('/GBT_Base_Manage/GBT_Base_Account/GetDataList', {}).then((resJson) => {
        if (resJson.Success) {
          this.AccountIdData = resJson.Data
        }
      })
    },
    openForm(id, title) {
      this.init()

      if (id) {
        this.loading = true
        this.$http.post('/GBT_Base_Manage/GBT_Base_ServiceItems/GetTheData', { id: id }).then((resJson) => {
          this.loading = false

          this.entity = resJson.Data
        })
      }
    },
    handleSubmit() {
      this.$refs['form'].validate((valid) => {
        if (!valid) {
          return
        }
        this.loading = true
        this.$http.post('/GBT_Base_Manage/GBT_Base_ServiceItems/SaveData', this.entity).then((resJson) => {
          this.loading = false

          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.visible = false

            this.parentObj.getDataList()
          } else {
            this.$message.error(resJson.Msg)
          }
        })
      })
    },
  },
}
</script>
